********************************************************************************************************************
*****Do-file for Tables A8-9 and A15-16
*****Who is mobilized to vote by short text messages? Evidence from a nationwide field experiment with young voters
********************************************************************************************************************
*****Last edited 24/6/5
********************************************************************************************************************
*****Ado packages needed: estout
********************************************************************************************************************

clear all

*Programs to calculate group differences with standard errors and stars
capture program drop, myrepost
program myrepost, eclass
ereturn repost b=`1'
ereturn repost V=`2'
ereturn scalar df_r=`3'
end

capture program drop mystars
program mystars, eclass
local d_stars=string(`1', "%9.3f")
local pval=ttail(`3',abs(`1'/`2'))*2

if `pval'<=0.01 {
local d_stars="`d_stars'"+"***"		
}

if `pval'>0.01 & `pval'<=0.05  {
local d_stars="`d_stars'"+"**"		
}

if `pval'>0.05 & `pval'<=0.1  {
local d_stars="`d_stars'"+"*"		
}

local se_stars=string(`2', "%9.3f")

local se_stars="("+"`se_stars'"+")"

ereturn local d_stars="`d_stars'"
ereturn local se_stars="`se_stars'"


end

*Use data
use \data\dataforanalysis230522_v2.dta, clear
*Set basecategory
fvset base 8 moses1d


label variable treatedf "Treated in HH"



*Generate voting propensity groups for direct effects
sum pvote_mother if voted22!=.  & treated!=. & female!=., detail
gen marginal=.
replace marginal=1 if pvote_mother>=r(p25) & pvote_mother<r(p75)
replace marginal=0 if pvote_mother<r(p25) | (pvote_mother>=r(p75) & pvote_mother!=.)

gen never=.
replace never=1 if pvote_mother<r(p25)
replace never=0 if pvote_mother>=r(p25) & pvote_mother!=.

gen always=.
replace always=1 if pvote_mother>=r(p75) & pvote_mother!=.
replace always=0 if pvote_mother<r(p75)

*Generate voting propensity groups for spillover effects
sum pvoteold if voted22!=.  & treatedf!=. & female!=., detail
gen marginalf=.
replace marginalf=1 if pvoteold>=r(p25) & pvoteold<r(p75)
replace marginalf=0 if pvoteold<r(p25) | (pvoteold>=r(p75) & pvoteold!=.)

gen neverf=.
replace neverf=1 if pvoteold<r(p25)
replace neverf=0 if pvoteold>=r(p25) & pvoteold!=.

gen alwaysf=.
replace alwaysf=1 if pvoteold>=r(p75) & pvoteold!=.
replace alwaysf=0 if pvoteold<r(p75)



*Generate dummies for individual and family member vote propensity groups
gen marginalfitemp=marginal if ntreatedf==1 & treated==1
replace marginalfitemp=marginal if ncontrolf==1 & treated==0
bys petu20: egen marginalfi=mean(marginalfitemp)

gen neverfitemp=never if ntreatedf==1 & treated==1
replace neverfitemp=never if ncontrolf==1 & treated==0
bys petu20: egen neverfi=mean(neverfitemp)


gen alwaysfitemp=always if ntreatedf==1 & treated==1
replace alwaysfitemp=always if ncontrolf==1 & treated==0
bys petu20: egen alwaysfi=mean(alwaysfitemp)

gen ftype=marginalf if treatedf!=.
replace ftype=2 if alwaysf==1 & treatedf!=.

gen fitype=marginalfi if treatedf!=.
replace fitype=2 if alwaysfi==1 & treatedf!=.


*************************************************
*****TABLE A15
*************************************************

tab ftype fitype, row






*************************************************
*****TABLE A16
*************************************************
*Panel A

eststo clear

eststo all: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf!=. & neverfi==1, cluster(kunta19)

estadd local controls "Yes"
estimates store all

qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: all

qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & neverfi==1, cluster(kunta19)

matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)
qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & neverfi==1, cluster(kunta19)

matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo low: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & neverfi==1, cluster(kunta19)
mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store low
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: low

qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & neverfi==1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & neverfi==1, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo marginalf: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & neverfi==1, cluster(kunta19)

mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- High"
estadd local controls "Yes"
estimates store marginalf
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: marginalf


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & neverfi==1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & neverfi==1, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo high: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & neverfi==1, cluster(kunta19)

mystars diffe stde df
estadd local label1 "High"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store high
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: high

local header "& \multicolumn{4}{c}{Outcome: Voted} \\"
local titles1 `"& All & "Low Propensity" & "Marginal Voters" & "High Propensity"\\ "'
local titles2 "& & Bottom 25\% & 25-75\% & Top 25\% \\"
local numbers "& (1) & (2) & (3) & (4) \\ \hline"
local emptyrow "& & & &  \\ "
local line "& & & & \hline \\ "

esttab using spill_combvprop.tex, keep (treatedf) noobs label se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("controls Controls " "umean Untreated $\bar{Y}$" "N Observations" "label1 \hline" "label2 \phantom{label2}" "d_stars Differences" "se_stars \phantom{se}") ///
sfmt(%9.3f %9.3f %9.0fc %9.3f) mlabels(none) nonumbers prehead(\begin{table}[htbp]\centering ///
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} ///
\caption{Spillovers by Combinations of Voting Propensity} ///
\begin{tabular}{l*{4}{c}}\hline\hline) posthead("`header'" "`emptyrow'" `"`titles1'"' "`titles2'" "`numbers'" \\ \multicolumn{4}{c}{\textbf{Panel A: Low Propensity Youth in HH }} \\ "`emptyrow'")  ///
refcat(treatedf "", nolabel below) postfoot(\hline) fragment nonotes replace
eststo clear


*Panel B


eststo all: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf!=. & marginalfi==1, cluster(kunta19)

estadd local controls "Yes"
estimates store all

qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: all

qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & marginalfi==1, cluster(kunta19)

matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)
qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & marginalfi==1, cluster(kunta19)

matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo low: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & marginalfi==1, cluster(kunta19)
mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store low
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: low

qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & marginalfi==1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & marginalfi==1, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo marginalf: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & marginalfi==1, cluster(kunta19)

mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- High"
estadd local controls "Yes"
estimates store marginalf
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: marginalf


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & marginalfi==1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & marginalfi==1, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo high: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & marginalfi==1, cluster(kunta19)

mystars diffe stde df
estadd local label1 "High"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store high
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: high

esttab using spill_combvprop.tex, keep (treatedf) noobs label se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("controls Controls " "umean Untreated $\bar{Y}$" "N Observations" "label1 \hline" "label2 \phantom{label2}" "d_stars Differences" "se_stars \phantom{se}") ///
sfmt(%9.3f %9.3f %9.0fc %9.3f) mlabels(none) nonumbers prefoot(\hline)  posthead("`emptyrow'" \multicolumn{4}{c}{\textbf{Panel B: Marginal Youth in HH}} \\ "`emptyrow'")  ///
refcat(treatedf "", nolabel below) fragment append nonotes 

*Panel C

eststo clear





eststo all: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf!=. & alwaysfi==1, cluster(kunta19)

estadd local controls "Yes"
estimates store all

qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: all

qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & alwaysfi==1, cluster(kunta19)

matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)
qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & alwaysfi==1, cluster(kunta19)

matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo low: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & alwaysfi==1, cluster(kunta19)
mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store low
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: low

qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & alwaysfi==1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & alwaysfi==1, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo marginalf: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if marginalf==1 & alwaysfi==1, cluster(kunta19)

mystars diffe stde df
estadd local label1 "Marginal"
estadd local label2 "- High"
estadd local controls "Yes"
estimates store marginalf
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: marginalf


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & alwaysfi==1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)


qui: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if neverf==1 & alwaysfi==1, cluster(kunta19)
matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])

eststo high: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if alwaysf==1 & alwaysfi==1, cluster(kunta19)

mystars diffe stde df
estadd local label1 "High"
estadd local label2 "- Low"
estadd local controls "Yes"
estimates store high
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: high



esttab, keep (treatedf) noobs label se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("controls Controls " "umean Untreated $\bar{Y}$" "N Observations" "label1 \hline" "label2 \phantom{label2}" "d_stars Differences" "se_stars \phantom{se}") ///
sfmt(%9.3f %9.3f %9.0fc %9.3f) mlabels(none) nonumbers prefoot(\hline)  posthead("`emptyrow'" \multicolumn{4}{c}{\textbf{Panel C: High Propensity Youth in HH}} \\ "`emptyrow'")  ///
refcat(treatedf "", nolabel below) postfoot(\hline\hline \\ \end{tabular} \\ \end{table}) fragment append nonotes 







*************************************************
*****TABLE A9
*************************************************

****************************************************************************************

replace age=age+3 //replace age for age in 2022

rename svatva_k income

reg voted22 treatedf molincome female age foreign i.moses1d firstvote i.mohighschool, cluster(kunta19)
keep if e(sample)==1

label variable female "Female"
label variable age "Age"
label variable mohighschool "High School Background"
label variable income "Taxable Income"
label variable foreign "Immigration Background"

eststo clear
eststo s1: estpost tabstat female age mohighschool income foreign if neverf==1,statistics(mean sd) columns(statistics)
eststo s2: estpost tabstat female age mohighschool income foreign if marginalf==1,statistics(mean sd) columns(statistics)
eststo s3: estpost tabstat female age mohighschool income foreign if alwaysf==1,statistics(mean sd) columns(statistics)


esttab using summary_spill.tex, cells(mean(fmt(2)) sd(fmt(2) par)) unstack nonote nonumber noobs stats(N, fmt(a) labels("Observations")) mtitles("Low Propensity" "Marginal Voters" "High Propensity") replace

*************************************************
*****TABLE A8
*************************************************


eststo clear

eststo all: estpost sum  female age mohighschool income foreign if voted22!=.
eststo control: estpost sum  female age mohighschool income foreign if voted22!=. & treatedf==0
eststo treated: estpost sum  female age mohighschool income foreign if voted22!=. & treatedf==1
eststo difference: estpost ttest  female age mohighschool income foreign if voted22!=., by(treatedf)


esttab all control treated difference using balance_spill.tex, replace mtitle("All" "Control" "Treated" "Difference") ///
cells( ///
mean(pattern(1 1 1 0) fmt(%9.3f)) & b(star pattern(0 0 0 1) fmt(%9.3f)) ///
sd(par pattern(1 1 1 0) fmt(%9.3f)) & se(par pattern(0 0 0 1) fmt(%9.3f))) ///
nonote nodepvars collabels("" "" "" "") nostar
